﻿using Quartz;
using Quartz.Impl;
using System;
using WebApp.App_Start;


namespace WebApp.Admins
{
    public partial class Schedule : System.Web.UI.Page
    {
        IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
        protected void Page_Load(object sender, EventArgs e)
        {
            status();
        }

        protected void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                Val.CMSPath = AppDomain.CurrentDomain.BaseDirectory;
                Job();
                status();
            }
            catch (Exception ex)
            {
                lblMeta.Text = ex.Message;
                if (ex.InnerException != null)
                    lblMeta.Text += ex.InnerException;
            }

        }

        protected void btnStop_Click(object sender, EventArgs e)
        {
            scheduler.Shutdown(true);
            status();
        }

        void status()
        {
            btnStart.Enabled = !scheduler.IsStarted;
            btnStop.Enabled = scheduler.IsStarted;

            txtInterval.Enabled = btnStart.Enabled;

            if (scheduler.IsStarted)
            {
                txtInterval.Text = Time.Minutes.ToString();

                var meta = scheduler.GetMetaData();
                lblMeta.Text =Val.CMSPath+ "-开始运行时间:" + meta.RunningSince.Value.AddHours(8).ToString("yyyy-MM-dd HH:mm:ss") + "\r\n运行次数:" + meta.NumberOfJobsExecuted;
            }
        }
        void Job()
        {
            Time.Minutes = int.Parse(txtInterval.Text);
            
            IJobDetail job1 = JobBuilder.Create<IndexJob>()  //创建一个作业
                .WithIdentity("作业名称", "作业组")
                .Build();
            ITrigger trigger1 = TriggerBuilder.Create()
                                        .WithIdentity("触发器名称", "触发器组")
                                        .StartNow()                        //现在开始
                                        .WithSimpleSchedule(x => x
                                            .WithIntervalInMinutes(Time.Minutes)
                                            //.WithIntervalInSeconds(Time.Minutes)
                                            .RepeatForever()//不间断重复执行
                                            )
                                        .Build();
            scheduler.ScheduleJob(job1, trigger1);
            scheduler.Start();
        }

        protected override void InitializeCulture()
        {
            base.InitializeCulture();
            if (Request.Cookies["CultureName"] != null)
            {
                var cuName = Request.Cookies["CultureName"].Value;
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cuName);
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cuName);
            }
        }
    }

    public static class Time
    {
        public static int Minutes { get; set; }
    }
}